package Common

import(
	"fmt"
	"encoding/json"
	"gin/Databases"
	"time"
)


func TimelineDefaultDate() (string,string) {
	time_now := time.Now()
	return time_now.AddDate(0, 0, -10).Format("2006-01-02"), time_now.AddDate(0, 0, 90).Format("2006-01-02")
	//var end_date = time_now.AddDate(0, 0, 90)
    //var date_str = time_now.Format("2006-01-02")
    //fmt.Println(date_str) //2020-04-26 05:44:24
    //fmt.Println(start_date) //2020-04-26 05:44:24
}

func TimelineHistoryDate(current string, offset int) (string,string) {
	t, err := time.Parse("2006-01-02", current)
	if err != nil {
		t = time.Now()
	}
	return t.AddDate(0, 0, -1 * offset).Format("2006-01-02"),t.Format("2006-01-02")
}

func TimelineFutureDate(current string, offset int) (string,string) {
	t, err := time.Parse("2006-01-02", current)
	if err != nil {
		t = time.Now()
	}
	return t.AddDate(0, 0, 1).Format("2006-01-02"),t.AddDate(0, 0, offset).Format("2006-01-02")
}

func TimelineOrderSql(data_type int, order_tag int) string {
	order := "asc"
	if order_tag == Order_desc {
		order = "desc"
	}
	if (data_type == Timeline_overall) {
		return "select Fdate,Ftype,Ftitle,Furl from macro_data where Fdate >= ? and Fdate <= ? order by Fdate " + order + ",Ftype;"
	} else if (data_type == Timeline_market) {
		return "select Fdate,Ftype,Ftitle,Furl from market_data where Fdate >= ? and Fdate <= ? order by Fdate " + order + ",Ftype;"
	} else {
		return ""
	}
}

func TimelineSql(data_type int) string {
	if (data_type == Timeline_overall) {
		return "select Fdate,Ftype,Ftitle,Furl from macro_data where Fdate >= ? and Fdate <= ? and Fvalid = 0 order by Fdate,Ftype;"
	} else if (data_type == Timeline_market) {
		return "select Fdate,Ftype,Ftitle,Furl from market_data where Fdate >= ? and Fdate <= ? and Fvalid = 0 order by Fdate,Ftype;"
	} else {
		return ""
	}

}


func AttrV3Sql(v3_type int) string {
	if (v3_type == AttrV3_susong) {
		return "select Fdate,Ftitle,Furl,Fsite from relation_data_03_susong where Fsrc_code = ? and Fsrc_type = 0 order by Fdate desc;"
	} else if (v3_type == AttrV3_chufa) {
		return "select Fdate,Ftitle,Furl,Fsite from relation_data_03_chufa where Fsrc_code = ? and Fsrc_type = 0 order by Fdate desc;"
	} else if (v3_type == AttrV3_tiaojia) {
		return "select Fdate,Ftitle,Furl,Fsite from relation_data_03_tiaojia where Fsrc_code = ? and Fsrc_type = 0 order by Fdate desc;"
	} else if (v3_type == AttrV3_diaoyan) {
                return "select Fdate,Ftitle,Furl,Fsite from relation_data_03_diaoyan where Fsrc_code = ? and Fsrc_type = 0 order by Fdate desc;"
        } else if (v3_type == AttrV3_chanyelian) {
                return "select Fdate,Ftitle,Furl,Fsite from relation_data_03_chanyelian where Fsrc_code = ? and Fsrc_type = 0 order by Fdate desc;"
        } else if (v3_type == AttrV3_guanli) {
                return "select Fdate,Ftitle,Furl,Fsite from relation_data_03_guanli where Fsrc_code = ? and Fsrc_type = 0 order by Fdate desc;"
        } else if (v3_type == AttrV3_yeji) {
                return "select Fdate,Ftitle,Furl,Fsite from relation_data_03_yeji where Fsrc_code = ? and Fsrc_type = 0 order by Fdate desc;"
        } else if (v3_type == AttrV3_other) {
		return "select Fdate,Ftitle,Furl,Fsite from relation_data_03_other where Fsrc_code = ? and Fsrc_type = 0 order by Fdate desc;"
	} else {
		return ""
	}
}

func GetV3Data(sql string, code string) (bool,string) {
	var datas []AttrV3Data
	err := Databases.Db.Select(&datas, sql, code)
	if err != nil {
		fmt.Println("exec failed, ", err)
		return false,""
	}

	if (len(datas) == 0) {
		return true, "{}"
	}

    str, _ := json.Marshal(datas)
    str_res := string(str)
    return true, str_res
}

func GetTimelineData(sql string, start_date string, end_date string) (bool, string) {
	var macros []MacroData
	//sql := "select Fdate,Ftype,Ftitle,Furl from market_data where Fdate >= ? and Fdate <= ? order by Fdate,Ftype;"
	err := Databases.Db.Select(&macros, sql, start_date, end_date)
	if err != nil {
		fmt.Println("exec failed, ", err)
		return false,""
	}

	fmt.Println("sql, ", sql)
	var macro_datas []MacroDataContent
	//var new_types []NewType

	var date_current = ""
	var type_current = 0
	var macro_data MacroDataContent
	var new_type NewType
	var datacnt = 0
	for i := 0; i < len(macros); i++ {
		if macros[i].Date != date_current {
			fmt.Println("date_current:", date_current)
			if date_current != "" {
				// slice必须手动进行深拷贝
				var type_temp NewType
				type_temp.Type = new_type.Type
				type_temp.News = make([]New, len(new_type.News[:len(new_type.News):len(new_type.News)]))
				copy(type_temp.News, new_type.News) 
				macro_data.Newtypes = append(macro_data.Newtypes, type_temp)

				// slice必须手动进行深拷贝
				var macro_data_temp MacroDataContent
				macro_data_temp.Date = macro_data.Date
				macro_data_temp.Newtypes = make([]NewType, len(macro_data.Newtypes[:len(macro_data.Newtypes):len(macro_data.Newtypes)]))
				copy(macro_data_temp.Newtypes, macro_data.Newtypes) 

		    	macro_datas = append(macro_datas, macro_data_temp)
		    	fmt.Println("title, ", macro_datas[0].Newtypes[0].News[0].Title)
		    	fmt.Println("date, ", macro_datas[0].Date)
		    	datacnt++
		    }
			macro_data.Date = macros[i].Date
			new_type.News = new_type.News[0:0]
			macro_data.Newtypes = macro_data.Newtypes[0:0]
			new_type.Type = GetOverallType(macros[i].Type)
			date_current = macros[i].Date
			type_current = macros[i].Type
		} else if type_current != macros[i].Type {
			// slice必须手动进行深拷贝
			var type_temp NewType
			type_temp.Type = new_type.Type
			type_temp.News = make([]New, len(new_type.News[:len(new_type.News):len(new_type.News)]))
			copy(type_temp.News, new_type.News)

			macro_data.Newtypes = append(macro_data.Newtypes, type_temp)
			new_type.Type = GetOverallType(macros[i].Type)
			new_type.News = new_type.News[0:0]
			type_current = macros[i].Type
		}
		var new = New{macros[i].Title, macros[i].Url}
		new_type.News = append(new_type.News, new)
    }
    if date_current != "" {
    	// slice必须手动进行深拷贝
		var type_temp NewType
		type_temp.Type = new_type.Type
		type_temp.News = make([]New, len(new_type.News[:len(new_type.News):len(new_type.News)]))
		copy(type_temp.News, new_type.News) 
		macro_data.Newtypes = append(macro_data.Newtypes, type_temp)
    	
    	// slice必须手动进行深拷贝
		var macro_data_temp MacroDataContent
		macro_data_temp.Date = macro_data.Date
		macro_data_temp.Newtypes = make([]NewType, len(macro_data.Newtypes[:len(macro_data.Newtypes):len(macro_data.Newtypes)]))
		copy(macro_data_temp.Newtypes, macro_data.Newtypes)
    	//macro_data.Newtypes = append(macro_data.Newtypes, macro_data_temp)

    	macro_datas = append(macro_datas, macro_data)
    }

    if (len(macro_datas) == 0) {
		return true, "{}"
	}

    str, _ := json.Marshal(macro_datas)
    str_res := string(str)
    return true, str_res
}
